package com.zq.assistant.ui.widget.recyclerview.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.animation.DecelerateInterpolator;

import com.zq.assistant.ui.widget.recyclerview.drag.ItemTouchHelperAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zhangqiang on 16-10-10.
 */

public abstract class BaseObjectRecyclerAdapter<T,V extends RecyclerView.ViewHolder> extends RecyclerHeaderFooterAdapter<V> implements ItemTouchHelperAdapter {

    private List<T> dataList;
    protected Context context;

    public BaseObjectRecyclerAdapter(List<T> dataList, Context context) {
        super();
        this.dataList = (dataList == null ? new ArrayList<T>() : dataList);
        this.context = context;
    }

    @Override
    public int getContentItemCount() {
        return this.dataList.size();
    }

    /**
     * 在指定位置添加数据
     * @param data
     * @param position
     */
    public void addDataAtIndex(T data,int position){

        final int previousCount = getItemCount();
        int headerCount = isHeaderEnable() ? getHeaderItemCount() : 0;
        this.dataList.add(position, data);
        notifyItemInserted(position + headerCount);
        if(position != previousCount - 1){

            notifyItemRangeChanged(position + headerCount,getItemCount() - position - headerCount);
        }
    }

    /**
     * 在指定位置插入数据列表
     * @param dataList
     * @param position
     */
    public void addDataListAtIndex(List<T> dataList,int position){

        if(dataList != null && !dataList.isEmpty()){

            final int previousCount = getItemCount();
            this.dataList.addAll(position, dataList);
            int headerCount = isHeaderEnable() ? getHeaderItemCount() : 0;
            notifyItemRangeInserted(position + headerCount,dataList.size());
            if(position != previousCount - 1){
                notifyItemRangeChanged(position + headerCount,getItemCount() - position - headerCount);
            }
        }
    }

    /**
     * 移除指定位置的数据
     * @param position
     */
    public void removeDataAtIndex(int position){

        final int headerCount = isHeaderEnable() ? getHeaderItemCount() : 0;
        this.dataList.remove(position);
        notifyItemRemoved(position + headerCount);
        if(position != getItemCount()){
            notifyItemRangeChanged(position + headerCount,getItemCount() - position - headerCount);
        }
    }

    /**
     * 获取数据的索引
     * @param data
     * @return
     */
    public int getDataIndex(T data){

        if(data == null){
            return -1;
        }
        return this.dataList.indexOf(data);
    }

    /**
     * 在末尾添加数据
     * @param data
     */
    public void addDataAtLast(T data){

        addDataAtIndex(data, this.dataList.size());
    }

    /**
     * 是否为空
     * @return
     */
    public boolean isEmpty(){

        return this.dataList != null && this.dataList.isEmpty();
    }

    /**
     * 在末尾插入数据列表
     * @param dataList
     */
    public void addDataListAtLast(List<T> dataList){

        addDataListAtIndex(dataList, this.dataList.size());
    }

    /**
     * 在开始位置添加数据
     * @param data
     */
    public void addDataAtFirst(T data){

        addDataAtIndex(data, 0);
    }

    /**
     * 在开始位置添加数据列表
     * @param dataList
     */
    public void addDataListAtFirst(List<T> dataList){

        addDataListAtIndex(dataList, 0);
    }


    /**
     * 移除一个数据
     * @param data
     */
    public void removeData(T data){

        int count = getItemCount();
        for (int i = 0; i < count; i++) {

            if(this.dataList.get(i).equals(data)){

                removeDataAtIndex(i);
                break;
            }
        }
    }

    public void removeAll(){

        this.dataList.clear();
        notifyDataSetChanged();
    }

    /**
     * 设置数据
     * @param dataList
     */
    public void setDataList(List<T> dataList){

        if(this.dataList == null){
            this.dataList = new ArrayList<T>();
        }

        this.dataList.clear();
        if(dataList != null){
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    /**
     * 获取指定位置的数据
     * @param position
     */
    public T getDataAt(int position){

        return this.dataList.get(position);
    }

    /**
     * 获取数据列表
     * @return
     */
    public List<T> getDataList(){

        return this.dataList;
    }


    public void removeDataList(List<T> dataList) {

        if(dataList != null && !dataList.isEmpty()){
            this.dataList.removeAll(dataList);
            notifyDataSetChanged();
        }
    }

    public ArrayList<T> getArrayDataList() {

        if(dataList instanceof ArrayList){
            return (ArrayList<T>) dataList;
        }
        return dataList == null ? new ArrayList<T>() : new ArrayList<>(dataList);
    }


    @Override
    public void onItemMove(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {

        int fromPosition = source.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        final int itemCount = getItemCount();
        if (fromPosition < itemCount && toPosition < itemCount) {
            //交换数据位置
            Collections.swap(dataList,fromPosition + getItemMovePositionOffset(),toPosition + getItemMovePositionOffset());

            //刷新位置交换
            notifyItemMoved(fromPosition, toPosition);

            notifyItemChanged(fromPosition);

            notifyItemChanged(toPosition);
        }
    }

    protected int getItemMovePositionOffset(){

        return isHeaderEnable() ? - getHeaderItemCount() : 0;
    }

    @Override
    public void onItemDismiss(RecyclerView.ViewHolder source) {
//        int position = source.getAdapterPosition();
//        dataList.remove(position); //移除数据
//        notifyItemRemoved(position);//刷新数据移除
    }

    @Override
    public void onItemSelect(RecyclerView.ViewHolder viewHolder) {
        //当拖拽选中时放大选中的view
        viewHolder.itemView.setScaleX(0.9f);
        viewHolder.itemView.setScaleY(0.9f);
    }

    @Override
    public void onItemClear(RecyclerView.ViewHolder viewHolder) {
        //拖拽结束后恢复view的状态
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(ObjectAnimator.ofFloat(viewHolder.itemView,"scaleX",1),
                ObjectAnimator.ofFloat(viewHolder.itemView,"scaleY",1));
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.setDuration(150);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.start();
    }

    @Override
    public boolean canItemDrag(RecyclerView.ViewHolder source) {
        return true;
    }


}
